"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A7检测项确保写入服务器日志的详细度配置检测与修复成功
Create At   : 2024/10/31
Owner       : lihongji
Description :
    1.设置log_error_verbosity参数为TERSE后检测A7项
    2.修复A7不符合安全项后再次检测,查看log_error_verbosity的值
    3.设置log_error_verbosity参数为VERBOSE后检测A7项
    4.修复A7不符合安全项后再次检测,查看log_error_verbosity的值
Expect      :
    1.设置成功，检测打印告警信息
    2.修复成功，不打印告警信息，log_error_verbosity值为default
    3.设置成功，检测打印告警信息
    4.修复成功，不打印告警信息，log_error_verbosity值为default
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        step = ('----step1:设置log_error_verbosity参数为TERSE后检测A7项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            "log_error_verbosity=TERSE")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A7 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(excute_msg)
        self.assertIn("[ Error reporting and logging configuration ]   "
                      "        : Warning", check_res, '执行失败' + step)

        step = ('----step2:修复A7不符合安全项再次检测,查看log_error_verbosity值'
                'expect:修复成功,不打印告警信息,log_error_verbosity值为default----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B7 --detail;'
                          f'gs_checkse -i A7 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show log_error_verbosity;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('default', create_res, '执行失败' + step)
        self.assertIn("[ Set Error reporting and logging configuration ]"
                      "       : Normal", check_res, '执行失败' + step)
        self.assertIn("[ Error reporting and logging configuration ]   "
                      "        : Normal", check_res, '执行失败' + step)

        step = ('----step3:设置log_error_verbosity参数为VERBOSE后检测A7项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            "log_error_verbosity=VERBOSE")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A7 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(excute_msg)
        self.assertIn("[ Error reporting and logging configuration ]   "
                      "        : Warning", check_res, '执行失败' + step)

        step = ('----step4:修复A7不符合安全项再次检测,查看log_error_verbosity值'
                'expect:修复成功,不打印告警信息,log_error_verbosity值为default----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B7 --detail;'
                          f'gs_checkse -i A7 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show log_error_verbosity;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('default', create_res, '执行失败' + step)
        self.assertIn("[ Set Error reporting and logging configuration ]"
                      "       : Normal", check_res, '执行失败' + step)
        self.assertIn("[ Error reporting and logging configuration ]   "
                      "        : Normal", check_res, '执行失败' + step)

    def tearDown(self):
        step = '----step5:清理环境 expect:成功----'
        self.log.info(step)
        revoke = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            "log_error_verbosity=default")
        self.assertTrue(revoke)
        self.log.info(f'----{os.path.basename(__file__)} end----')